﻿using HW_PlcLib;
using HW_PlcLib.Enums;
using HW_PlcLib.Model;
using Plc_Communication_Demo.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Plc_Communication_Demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private MainViewModel MainViewModel;

        public event PropertyChangedEventHandler PropertyChanged;

        private KeyencePlc KeyencePlc;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public MainWindow()
        {
            InitializeComponent();
            MainViewModel = DataContext as MainViewModel;
            KeyencePlc = MainViewModel.KeyencePlc;
        }


        private void Button_OpenPLC_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Net net = new Net();
                net.LocalIP = "127.0.0.1";
                net.LocalPort = 502;
                
                net.IP = "192.168.3.249";
                net.Port = 8501;

                net.IP = MainViewModel.InputAddressText != null ? MainViewModel.InputAddressText : "192.168.3.249";
                
                net.Port = MainViewModel.InputPortText != null ?  int.Parse(MainViewModel.InputPortText) : 8501;

                KeyencePlc.Initialize(net);//初始化PLC

                KeyencePlc.Start();
            });

        }

        private void Button_ClosePLC_Click(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Stop();
        }

        private void Button_Click_SET(object sender, RoutedEventArgs e)
        {
            KeyencePlc.SET_Addr(MainViewModel.InputCommandText);
        }

        private void Button_Click_RES(object sender, RoutedEventArgs e)
        {
            KeyencePlc.RES_Addr(MainViewModel.InputCommandText);
        }

        private void Button_Click_Bit(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Read_Bit(MainViewModel.InputCommandText);
        }

        private void Button_Click_UInt16(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Read_UInt16(MainViewModel.InputCommandText);
        }

        private void Button_Click_Int16(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Read_Int16(MainViewModel.InputCommandText);
        }

        private void Button_Click_UInt32(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Read_Int16(MainViewModel.InputCommandText);
        }

        private void Button_Click_Int32(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Read_Int32(MainViewModel.InputCommandText);
        }

        private void Button_Click_Float(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Read_Float(MainViewModel.InputCommandText);
        }

        private void Button_Click_String(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Read_String(MainViewModel.InputCommandText);
        }

        private void Button_Click_W_UInt16(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Write_UInt16(MainViewModel.InputCommandText, MainViewModel.InputDataText);
        }

        private void Button_Click_W_Int16(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Write_Int16(MainViewModel.InputCommandText, MainViewModel.InputDataText);
        }

        private void Button_Click_W_UInt32(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Write_Int16(MainViewModel.InputCommandText, MainViewModel.InputDataText);
        }

        private void Button_Click_W_Int32(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Write_Int32(MainViewModel.InputCommandText, MainViewModel.InputDataText);
        }

        private void Button_Click_W_Float(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Write_Float(MainViewModel.InputCommandText, MainViewModel.InputDataText);
        }

        private void Button_Click_W_String(object sender, RoutedEventArgs e)
        {
            KeyencePlc.Write_String(MainViewModel.InputCommandText, MainViewModel.InputDataText);
        }
    }
}
